<?php
/**
* エラーチェッククラス
**/
require_once( '../system/Class_M_DAO.php' );
class Class_M_MATRIX extends Class_M_DAO {
	//protected properties begin
	/**
	 *
	 * テーブル名
	 *
	 * @access    protected
	 * @var       string
	 *
	 */
	protected $_table = "M_MATRIX_TERM";
	
	/**
	 *
	 * 主キー名
	 *
	 * @access    protected
	 * @var       string
	 *
	 */
	protected $_pkey = "matrix_cd";
	
	/**
	 *
	 * ソート順
	 *
	 * @access    protected
	 * @var       string
	 *
	 */
	 protected $_order = "matrix_cd";
	 
	 protected $_cols     = array(
		"matrix_cd"               => array(AOKI_VARTYPE_STR),
		"year4"                   => array(AOKI_VARTYPE_STR),
		"week_cd"                 => array(AOKI_VARTYPE_STR),
		"week_name"               => array(AOKI_VARTYPE_STR),
		"week_seq"                => array(AOKI_VARTYPE_STR),
		"week_date_begin"         => array(AOKI_VARTYPE_STR),
		"week_date_end"           => array(AOKI_VARTYPE_STR),
		"flier_date"              => array(AOKI_VARTYPE_STR),
		"draft_create_dt"         => array(AOKI_VARTYPE_STR),
		//"draft_input_begin_dt"    => array(AOKI_VARTYPE_STR),
		"draft_input_end_dt"      => array(AOKI_VARTYPE_STR),
		//"draft_modi_begin_dt"     => array(AOKI_VARTYPE_STR),
		"draft_modi_end_dt"       => array(AOKI_VARTYPE_STR),
		//"draft_publish_begin_dt"  => array(AOKI_VARTYPE_STR),
		"draft_publish_end_dt"    => array(AOKI_VARTYPE_STR),
		//"final_input_begin_dt"    => array(AOKI_VARTYPE_STR),
		"final_input_end_dt"      => array(AOKI_VARTYPE_STR),
		//"final_modi_begin_dt"     => array(AOKI_VARTYPE_STR),
		"final_modi_end_dt"       => array(AOKI_VARTYPE_STR),
		"final_publish_begin_dt"  => array(AOKI_VARTYPE_STR)
		
	 );
	
	 protected $_checks  = array(
		"year4"                   => array( "EXIST", "NUM"),
		"week_cd"                 => array( "EXIST"),		
		"week_seq"                => array( "NUM"),
		"week_date_begin"         => array( "EXIST"),
		"week_date_end"           => array( "EXIST"),
		"flier_date"              => array( "EXIST"),
		"draft_create_dt"         => array( "EXIST"),
	//	"draft_input_begin_dt"    => array( "EXIST"),
		"draft_input_end_dt"      => array( "EXIST"),
	//	"draft_modi_begin_dt"     => array( "EXIST"),
		"draft_modi_end_dt"       => array( "EXIST"),
	//	"draft_publish_begin_dt"  => array( "EXIST"),
		"draft_publish_end_dt"    => array( "EXIST"),
	//	"final_input_begin_dt"    => array( "EXIST"),
		"final_input_end_dt"      => array( "EXIST"),
	//	"final_modi_begin_dt"     => array( "EXIST"),
		"final_modi_end_dt"       => array( "EXIST"),
	//	"final_publish_begin_dt"  => array( "EXIST")
	 );
	 
	 //maxlength 
	 protected $_maxlens  = array(
		"year4"         =>  4 ,
		"week_seq"      => 1
	
	 );
	
	 public function search($where ,&$page = NULL ){
		$sql = "SELECT b.*,CURRENT_TIMESTAMP as sysd, (select  user_name FROM M_LOGIN where id  = b.u_user) as m_user_name from {$this->_table}  b ";
		$cnt_sql = "SELECT COUNT(*) AS cnt FROM {$this->_table} b ";
		return  parent::search($where ,$page ,$sql, $cnt_sql);
	}
	
	 public function searchcsv($where ){
		$sql = "SELECT a.*,p.pattern_name,CURRENT_TIMESTAMP as sysd, (select  user_name FROM M_LOGIN where id  = a.u_user) as m_user_name," .
				" b.year4,b.week_cd,b.week_name,b.week_seq,b.flier_date,b.week_date_begin,b.week_date_end,b.pre_matrix_cd,b.newyear_flag," .
				" b.draft_create_dt,b.draft_input_end_dt," .
				" b.draft_modi_end_dt,b.draft_publish_end_dt,b.final_input_end_dt,b.final_modi_end_dt " .
				" from M_MATRIX_VER  a inner join M_MATRIX_TERM as b on a.matrix_cd = b.matrix_cd " .
				" left join M_PATTERN p on p.pattern_cd = a.pattern_cd" ;
		$orderby ="  a.matrix_cd,a.groupid";
		$page = NULL;
		
		return  parent::search($where, $page, $sql, NULL, $orderby );
	}
	
	function droptable( $inmatrixid ){
		$tab_m_ins_shop_shop= "M_INS_SHOP_SHOP".$inmatrixid ;
		$tab_m_dnpw_area      = "D_DNPW_AREA".$inmatrixid ;
		$tab_m_dnpw_area_shop = "D_DNPW_AREA_SHOP".$inmatrixid ;
		
		
		$tab_m_t_pattern_d  = "T_PATTERN_D".$inmatrixid ;	
		$tab_m_t_pattern  = "T_PATTERN".$inmatrixid ;
			
		$this->begin_trans();
		
		try{	
			$sql =" drop table {$tab_m_ins_shop_shop} ";
			$ret =  $this->_db->query( $sql );
			if (!$ret){
				throw  new Exception("{$tab_m_ins_shop_shop}削除失敗しました");
			}

			$sql =" drop table {$tab_m_dnpw_area} ";
			$ret =  $this->_db->query( $sql );
			if (!$ret){
				throw  new Exception("{$tab_m_dnpw_area}削除失敗しました");
			}
			
			$sql =" drop table {$tab_m_dnpw_area_shop} ";
			$ret =  $this->_db->query( $sql );
			if (!$ret){
				throw  new Exception("{$tab_m_dnpw_area_shop}削除失敗しました");
			}

			$sql =" drop table {$tab_m_t_pattern_d} ";
			$ret =  $this->_db->query( $sql );
			if (!$ret){
				throw  new Exception("{$tab_m_t_pattern_d}削除失敗しました");
			}

			$sql =" drop table {$tab_m_t_pattern} ";
			$ret =  $this->_db->query( $sql );
			if (!$ret){
				throw  new Exception("{$tab_m_t_pattern}削除失敗しました");
			}
			
			$sql = "update {$this->_table} set week_data_flg = 9 where matrix_cd = {$inmatrixid}";
			$ret =  $this->_db->query( $sql );
			if (!$ret){
				throw  new Exception("{$inmatrixid}作成失敗しました");
			}
			$this->commit_trans();
			return false;
		
		} catch (Exception $e){
			print_r($e); 
			$this->rollback_trans();
			return false;
		}
	}
		
	function createtable( $inmatrixid ){
		
		$tab_m_dnpw_area      = "D_DNPW_AREA".$inmatrixid ;
		$tab_m_dnpw_area_shop = "D_DNPW_AREA_SHOP".$inmatrixid ;
		
		$tab_m_ins_shop_shop  = "M_INS_SHOP_SHOP".$inmatrixid ;
		
		$tab_m_t_pattern_d  = "T_PATTERN_D".$inmatrixid ;
		
		$tab_m_t_pattern  = "T_PATTERN".$inmatrixid ;
		//insert into T_PATTERN_D2012010 select * from T_PATTERN_D
		//M_INS_SHOP_SHOP2012010
		//T_PATTERN_D2012010
		
		
		$this->begin_trans();
		try{
			$sql ="
			CREATE TABLE IF NOT EXISTS `{$tab_m_ins_shop_shop}` (
		  `ID` int(11) NOT NULL AUTO_INCREMENT ,
		  `ins_shop_shop_cd` varchar(30) NOT NULL ,
		  `shop_cd` varchar(11) NOT NULL ,
		  `ins_shop_cd` varchar(25) NOT NULL ,
		  `insert_conums` int(11) DEFAULT NULL,
		  `memo` varchar(300) DEFAULT NULL,
		  `disp_ng` varchar(1) DEFAULT NULL ,
		  `area_pattern_cd` int(11) DEFAULT NULL,
		  `area_adnums` int(11) DEFAULT NULL,
		  `area_memo` varchar(300) DEFAULT NULL,
		  `area_disp_ng` varchar(1) DEFAULT NULL,
		  `u_time` datetime NOT NULL,
		  `u_user` int(11) NOT NULL ,
		  `c_time` datetime NOT NULL,
		  `c_user` int(11) NOT NULL ,
		   PRIMARY KEY (`ID`)
		   ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=70 ";
		   
			$ret =  $this->_db->query( $sql );
			if (!$ret){
				throw  new Exception("{$tab_m_ins_shop_shop}作成失敗しました");
			}
			
			$sql ="CREATE TABLE IF NOT EXISTS `{$tab_m_dnpw_area}` (
		  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自動ＩＤ',
		  `dnpid` int(11) NOT NULL,
		  `pattern_cd` int(11) NOT NULL COMMENT 'パターンコード',
		  `dist_id` int(11) DEFAULT NULL COMMENT '折込エリアID',
		  `area_nums` int(11) DEFAULT NULL COMMENT 'エリア部数',
		  `title_cd` int(11) DEFAULT NULL COMMENT 'タイトルコード',
		  `hand_cd` int(11) DEFAULT NULL COMMENT '業者コード',
		  `center_flg` char(1) DEFAULT NULL COMMENT '都心エリア',
		  `memo` varchar(256) DEFAULT NULL COMMENT '備考',
		  `u_time` datetime NOT NULL COMMENT '更新日時',
		  `u_user` int(11) NOT NULL COMMENT '更新者名',
		  `c_time` datetime NOT NULL COMMENT '新規日時',
		  `c_user` int(11) NOT NULL COMMENT '新規者名',
		  PRIMARY KEY (`id`)
		) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=240 ";
			$ret =  $this->_db->query( $sql );
			if (!$ret){
				throw  new Exception("{$tab_m_dnpw_area}作成失敗しました");
			}
			
			$sql ="
CREATE TABLE IF NOT EXISTS `{$tab_m_dnpw_area_shop}` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自動ＩＤ',
  `dnpid` int(11) NOT NULL COMMENT 'DNPエリアID',
  `pattern_cd` int(11) DEFAULT NULL,
  `shop_cd` varchar(11) NOT NULL COMMENT '店舗コード',
  `shop_per` int(11) NOT NULL COMMENT '個店部数割当',
  `shop_nums` int(11) DEFAULT '0' COMMENT '個店按分部数',
  `ins_nums` int(11) DEFAULT '0' COMMENT '販売店数',
  `map_no` int(11) DEFAULT NULL,
  `u_time` datetime NOT NULL COMMENT '更新日時',
  `u_user` int(11) NOT NULL COMMENT '更新者名',
  `c_time` datetime NOT NULL COMMENT '新規日時',
  `c_user` int(11) NOT NULL COMMENT '新規者名',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=331 ";
			$ret =  $this->_db->query( $sql );
			if (!$ret){
				throw  new Exception("{$tab_m_dnpw_area_shop}作成失敗しました");
			}

			$sql ="
CREATE TABLE IF NOT EXISTS `{$tab_m_t_pattern_d}` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自動ID',
  `ins_shop_shop_cd` varchar(30) NOT NULL COMMENT '店舗販売店コード',
  `pattern_cd` int(11) NOT NULL COMMENT 'パターンコード',
  `pattern_name` varchar(30) NOT NULL COMMENT 'パターン名',
  `pnums` int(11) NOT NULL COMMENT '部数',
  PRIMARY KEY (`id`),
UNIQUE KEY `ins_shop_shop_cd_UNIQUE` (`ins_shop_shop_cd`),
  KEY `ptt` (`ins_shop_shop_cd`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=0 ; ";
			$ret =  $this->_db->query( $sql );
			if (!$ret){
				throw  new Exception("{$tab_m_t_pattern_d}作成失敗しました");
			}
			
$sql ="
CREATE TABLE IF NOT EXISTS `{$tab_m_t_pattern}` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自動ID',
  `ins_shop_shop_cd` varchar(30) NOT NULL COMMENT '店舗販売店コード',
  `shop_cd` varchar(5) DEFAULT NULL COMMENT '店舗コード',
  `ins_shop_cd` varchar(25) DEFAULT NULL COMMENT '販売店舗コード',
  `pper` int(11) NOT NULL COMMENT '部数パセント',
  `pnums` int(11) NOT NULL COMMENT '部数',
  `pmemo` varchar(500) DEFAULT NULL COMMENT '備考',
  `u_time` datetime NOT NULL COMMENT '更新日時',
  `u_user` int(11) NOT NULL COMMENT '更新者名',
  `c_time` datetime NOT NULL COMMENT '新規日時',
  `c_user` int(11) NOT NULL COMMENT '新規者名',
  PRIMARY KEY (`id`),
  UNIQUE KEY `ins_shop_shop_cd_UNIQUE` (`ins_shop_shop_cd`),
  KEY `ptt` (`ins_shop_shop_cd`),
  KEY `ptt_shop` (`shop_cd`),
  KEY `ptt_insshop` (`ins_shop_cd`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=0 ;";
			$ret =  $this->_db->query( $sql );
			if (!$ret){
				throw  new Exception("{$tab_m_t_pattern}作成失敗しました");
			}
			
			$sql = "update {$this->_table} set week_data_flg = 1 where matrix_cd = {$inmatrixid}";
			$ret =  $this->_db->query( $sql );
			if (!$ret){
				throw  new Exception("{$inmatrixid}作成失敗しました");
			}
			$this->commit_trans();
			return true;
		
		} catch (Exception $e){ 
			$this->rollback_trans();
			print_r( $e );
			return false;
		}
	}
	public function getVerCount($value) {
		$sql = "SELECT count(*)  as cnt FROM M_MATRIX_VER WHERE matrix_cd = '{$value}'";
		$this->_db->query( $sql );
		if( $this->_db->num_rows() ){
			$row = $this->_db->fetch();
			return $row['cnt'];
		}else{
			return  null;
		}
	}

		
		
	public function getStatues($row) {
		$nowdate = $row["sysd"];
		if ($nowdate <= $row["draft_create_dt"]){
			return  "Draft作成中";
		} else if ($nowdate <= $row["draft_input_end_dt"]) {
			return  "Draft入力中";
		} else if ($nowdate <= $row["draft_modi_end_dt"]) {
			return  "Draft確認．修正中";
		} else if ($nowdate <= $row["draft_publish_end_dt"]) {
			return  "Draft手配中";
		} else if ($nowdate <= $row["final_input_end_dt"]) {
			return  "Final入力中";
		} else if ($nowdate <= $row["final_modi_end_dt"]) {
			return  "Final確認．修正中";
		} else {
			return  "Final済";
		}
	}

}

?>